package com.guigu.eduservice.controller;

import com.guigu.common.utils.R;
import com.guigu.eduservice.dto.DetectionReportRequest;
import com.guigu.eduservice.entity.DetectionReport;
import com.guigu.eduservice.service.DetectionReportService;
import com.guigu.eduservice.utils.EmployeeJwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.R;
import com.guigu.eduservice.entity.DetectionReport;
import com.guigu.eduservice.service.DetectionReportService;
import com.guigu.eduservice.vo.DetectionReportVo;
import com.guigu.eduservice.vo.EvaluationInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 检测报告表（存储设备检测的结果及结论） 前端控制器
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
@RestController
@RequestMapping("/eduservice/detectionReport")
public class DetectionReportController {
    @Autowired
    private DetectionReportService detectionReportService;
    //修改预约
    @GetMapping("AddDetectionReport1/{date}/{equipmentId}")
    public R selectDetectionReportById1(HttpServletRequest request, @PathVariable("date")  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") LocalDateTime date, @PathVariable("equipmentId") Long equipmentId) {
        int businessmanId = EmployeeJwtUtils.getUserIDByJwtToken(request);
        DetectionReport detectionReport = new DetectionReport();
        detectionReport.setEquipmentId(equipmentId);
        detectionReport.setBusinessmanId((long) businessmanId);
        detectionReport.setDetectionTime(date);
        detectionReport.setDetectionConclusion("待检测");
        detectionReport.setAuditStatus(0);
        boolean save = detectionReportService.save(detectionReport);
        if ( save){
            return R.ok().message("修改预约成功");
        }
        return R.error().message("修改预约失败");
    }
    //修改预约
    @PostMapping("AddDetectionReport")
    public R selectDetectionReportById(HttpServletRequest request, @RequestBody DetectionReportRequest detectionReportRequest) {
        int businessmanId = EmployeeJwtUtils.getUserIDByJwtToken(request);
        DetectionReport detectionReport = new DetectionReport();
        detectionReport.setEquipmentId(detectionReportRequest.getEquipmentId());
        detectionReport.setBusinessmanId((long) businessmanId);
        detectionReport.setDetectionTime(detectionReportRequest.getDate());
        detectionReport.setDetectionConclusion("待检测");
        detectionReport.setAuditStatus(0);
        boolean save = detectionReportService.save(detectionReport);
        if ( save){
            return R.ok().message("修改预约成功");
        }
        return R.error().message("修改预约失败");
    }
    //根据设备ID查询检测信息
    @GetMapping("/selectDetectionReportById/{EId}")
    public R selectDetectionReportById(@PathVariable("EId") Long EId) {
        DetectionReport report = detectionReportService.selectDetectionReportById(EId);
        return R.ok().data("data",report);
    }
    /**
     * 创建检测报告
     * @param reportVo 检测报告信息
     * @return 是否成功
     */
    @PostMapping("/create")
    public R createDetectionReport(@RequestBody DetectionReportVo reportVo) {
        boolean result = detectionReportService.createDetectionReport(reportVo);
        if (result) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    /**
     * 分页查询评估信息
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return 分页结果
     */
    @PostMapping("/page/{pageNum}/{pageSize}")
    public PageInfo<EvaluationInfoVo> getEvaluationInfoList(
            @RequestBody DetectionReportVo vo,
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize
    ) {
        return detectionReportService.getEvaluationInfoList(vo,pageNum, pageSize);
    }

    @PostMapping("/update")
    public int updateDetectionReport(@RequestBody DetectionReportVo reportVo) {
        int result = detectionReportService.updateDetectionReport(reportVo);
        return result;
    }

}
